package com.example.controller;

import com.example.entity.Commodit;
import com.example.service.ICommoditService;
import com.example.util.Code;
import com.example.util.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/s/commodit")
@CrossOrigin
public class CommoditController {
    @Autowired
    private ICommoditService iCommoditService;

    //通过商店id查询店内商品及其信息
    @PostMapping("/find")
    public Result findAll(@RequestBody Commodit commodit){
        return new Result(Code.GET_OK,iCommoditService.findAll(commodit),"查询成功");
    }
    //修改商品上下架状态
    @GetMapping("/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Integer id, @PathVariable("status")Integer status){
        if(status==17){
            iCommoditService.updateStatus1(id);
            return new Result(Code.GET_OK,"上架成功");
        }else {
            iCommoditService.updateStatus2(id);
            return new Result(Code.GET_ERR,"下架成功");
        }
    }
    //添加商品
    @PostMapping("/add")
    public Result addCommodit(@RequestBody Commodit commodit){
        int i = iCommoditService.addCommodit(commodit);
        if(i>0){
            return new Result(Code.GET_OK,commodit.getStore(),"成功");
        }else {

            return new Result(Code.GET_ERR,"失败");
        }
    }

    /**
     * 根据id查商品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findalls(@PathVariable Integer id){
        List<Map> mapList = iCommoditService.findall(id);
        if(mapList!=null && mapList.size()>0){
            return new Result(Code.GET_OK,mapList,"查询成功");
        }else{
            return new Result(Code.GET_ERR,"查询失败");
        }
    }
//TTT根据id查询单个加油站商品名称和地址
    @PostMapping("/findByid/{id}")
    public Result findByid(@PathVariable Integer id){
        Map byid = iCommoditService.findByid(id);
        if(byid!=null && byid.keySet().size()>0){
            return new Result(Code.GET_OK,byid,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }
//TTT查询所有加油站商品名称和地址
    @GetMapping("/findAll")
    public Result findAll() {
        List<Map> all = iCommoditService.findAll();
        if (all != null) {
            return new Result(Code.GET_OK, all, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }




}
